//@author A0092201X
#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace TextManipulatorClassTest
{		
	TEST_CLASS(TextManipulatorUnitTests)
	{
	public:
		
		TEST_METHOD(TestReadFunctionality_Empty)
		{
			std::vector<Task> taskVector;
			Manipulator* tmPtr;
			tmPtr = new TextManipulator("TestReadFunctionality.txt");			
			taskVector = tmPtr->read();

			Assert::IsTrue(taskVector.empty()); 

			delete tmPtr;
		}


		TEST_METHOD(TestReadFunctionality_Filled)
		{
			std::vector<Task> taskVector;
			Manipulator* tmPtr;
			tmPtr = new TextManipulator("TestReadFunctionality_Filled.txt");			
			taskVector = tmPtr->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", taskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", taskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", taskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", taskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", taskVector[4].prepareLine());
			Assert::AreEqual(0, taskVector[0].getIndex());
			Assert::AreEqual(1, taskVector[1].getIndex());
			Assert::AreEqual(2, taskVector[2].getIndex());
			Assert::AreEqual(3, taskVector[3].getIndex());
			Assert::AreEqual(4, taskVector[4].getIndex());			
			Assert::AreEqual((size_t) 5, taskVector.size());

			delete tmPtr;
		}

		TEST_METHOD(TestWriteFunctionality)
		{
			std::vector<Task> taskVector;
			Manipulator* tmPtr1;
			Manipulator* tmPtr2;
			
			tmPtr1 = new TextManipulator("TestReadFunctionality_Filled.txt");
			tmPtr2 = new TextManipulator("TestWriteFunctionality.txt");
			
			taskVector = tmPtr1->read();
			tmPtr2->write(taskVector);
			taskVector = tmPtr2->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", taskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", taskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", taskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", taskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", taskVector[4].prepareLine());
			Assert::AreEqual(0, taskVector[0].getIndex());
			Assert::AreEqual(1, taskVector[1].getIndex());
			Assert::AreEqual(2, taskVector[2].getIndex());
			Assert::AreEqual(3, taskVector[3].getIndex());
			Assert::AreEqual(4, taskVector[4].getIndex());			
			Assert::AreEqual((size_t) 5, taskVector.size());		

			delete tmPtr1;
			delete tmPtr2;
		}

		TEST_METHOD(TestAppendFunctionality)
		{
			std::vector<Task> taskVector;
			std::vector<Task> appendedTaskVector;
			Manipulator* tmPtr1;
			Manipulator* tmPtr2;
			
			tmPtr1 = new TextManipulator("TestReadFunctionality_Filled.txt");
			tmPtr2 = new TextManipulator("TestAppendFunctionality.txt");			
			
			taskVector = tmPtr1->read();

			tm structTime;
			structTime.tm_year = 100 + 14;
			structTime.tm_mon = 2 - 1;
			structTime.tm_mday = 14;
			structTime.tm_hour = 17;
			structTime.tm_min = 0;
			structTime.tm_sec = 0;
			time_t timeT = mktime(&structTime);
		
			tmPtr2->write(taskVector);	
			Task task("Meet up with Gf till dunno what time", timeT, (time_t)0, "Concord Hotel");
			tmPtr2->append(task);
			appendedTaskVector = tmPtr2->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", taskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", taskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", taskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", taskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", taskVector[4].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", appendedTaskVector[5].prepareLine());
			Assert::AreEqual(0, appendedTaskVector[0].getIndex());
			Assert::AreEqual(1, appendedTaskVector[1].getIndex());
			Assert::AreEqual(2, appendedTaskVector[2].getIndex());
			Assert::AreEqual(3, appendedTaskVector[3].getIndex());
			Assert::AreEqual(4, appendedTaskVector[4].getIndex());	
			Assert::AreEqual(5, appendedTaskVector[5].getIndex());	
			Assert::AreEqual((size_t) 6, appendedTaskVector.size());

			delete tmPtr1;
			delete tmPtr2;
		}

		TEST_METHOD(TestSortFunctionality_NoChange)
		{
			std::vector<Task> taskVector;
			std::vector<Task> sortedTaskVector;
			Manipulator* tmPtr1;
			Manipulator* tmPtr2;
			
			tmPtr1 = new TextManipulator("TestReadFunctionality_Filled.txt");
			tmPtr2 = new TextManipulator("TestSortFunctionality_NoChange.txt");			
			
			taskVector = tmPtr1->read();		
			tmPtr2->write(taskVector);

			tmPtr2->sort();
			
			sortedTaskVector = tmPtr2->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", taskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", taskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", taskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", taskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", taskVector[4].prepareLine());
			Assert::AreEqual(0, taskVector[0].getIndex());
			Assert::AreEqual(1, taskVector[1].getIndex());
			Assert::AreEqual(2, taskVector[2].getIndex());
			Assert::AreEqual(3, taskVector[3].getIndex());
			Assert::AreEqual(4, taskVector[4].getIndex());			
			Assert::AreEqual((size_t) 5, taskVector.size());

			delete tmPtr1;
			delete tmPtr2;
		}

		TEST_METHOD(TestSortFunctionality_Changes)
		{
			std::vector<Task> taskVector;
			std::vector<Task> sortedTaskVector;
			Manipulator* tmPtr1;
			Manipulator* tmPtr2;
			
			tmPtr1 = new TextManipulator("TestSortFunctionality_Changes(before).txt");
			tmPtr2 = new TextManipulator("TestSortFunctionality_Changes(after).txt");			
			
			taskVector = tmPtr1->read();		
			tmPtr2->write(taskVector);

			tmPtr2->sort();
			
			sortedTaskVector = tmPtr2->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", sortedTaskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", sortedTaskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", sortedTaskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", sortedTaskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", sortedTaskVector[4].prepareLine());
			Assert::AreEqual(0, taskVector[0].getIndex());
			Assert::AreEqual(1, taskVector[1].getIndex());
			Assert::AreEqual(2, taskVector[2].getIndex());
			Assert::AreEqual(3, taskVector[3].getIndex());
			Assert::AreEqual(4, taskVector[4].getIndex());			
			Assert::AreEqual((size_t) 5, taskVector.size());

			delete tmPtr1;
			delete tmPtr2;
		}

		TEST_METHOD(TestSortFunctionality_Invalid)
		{
			std::vector<Task> taskVector;
			std::vector<Task> sortedTaskVector;
			Manipulator* tmPtr1;
			Manipulator* tmPtr2;
			
			tmPtr1 = new TextManipulator("TestSortFunctionality_Invalid(before).txt");
			tmPtr2 = new TextManipulator("TestSortFunctionality_Invalid(after).txt");		

			taskVector = tmPtr1->read();		
			tmPtr2->write(taskVector);

			tmPtr2->sort();
			
			sortedTaskVector = tmPtr2->read();

			Assert::AreEqual((std::string) "[F]\tBuy flowers for valentine's day\t-\t-\t-", sortedTaskVector[0].prepareLine());
			Assert::AreEqual((std::string) "[T]\tMeet up with Ex before Vday\t13/02/14 1900\t13/02/14 2300\tHotel Rendezous", sortedTaskVector[1].prepareLine());
			Assert::AreEqual((std::string) "[D]\tOrder Lambogini for valentine\t-\t14/02/14 0000\t-", sortedTaskVector[2].prepareLine());
			Assert::AreEqual((std::string) "[T]\tPacify wife\t14/02/14 0800\t14/02/14 1000\tin-law's place", sortedTaskVector[3].prepareLine());
			Assert::AreEqual((std::string) "[P]\tMeet up with Gf till dunno what time\t14/02/14 1700\t-\tConcord Hotel", sortedTaskVector[4].prepareLine());
			Assert::AreEqual(0, sortedTaskVector[0].getIndex());
			Assert::AreEqual(1, sortedTaskVector[1].getIndex());
			Assert::AreEqual(2, sortedTaskVector[2].getIndex());
			Assert::AreEqual(3, sortedTaskVector[3].getIndex());
			Assert::AreEqual(4, sortedTaskVector[4].getIndex());			
			Assert::AreEqual((size_t) 5, sortedTaskVector.size());

			delete tmPtr1;
			delete tmPtr2;
		}
	};
}